function getUrlRequest(){
        var url = location.search; //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            if (str.indexOf("&") != -1) {
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            } else {
                var key = str.substring(0,str.indexOf("="));
                var value = str.substr(str.indexOf("=")+1);
                theRequest[key] = decodeURI(value);
            }
        }
        return theRequest;
    }

	var datagrid;
	var Request = new Object();
    Request = getUrlRequest();
	var testStandardID;
	var itemName='';
	testStandardID = Request["id"];
	//alert(tempID);
	var rowEditor=undefined;
	var editRow=undefined;
	var editUrl="";

	var keditor;
	KindEditor.ready(function(K) {//textarea[name="content"]
		keditor = K.create('#content', {
					allowFileManager : true,
					width:'0px',
					height:'0px'
				});	
	});
	
	$(function(){
		$('#contentDiv').hide();
		if (testStandardID==''||testStandardID==null) {
			testStandardID=0;
		};
		datagrid=$("#dg").datagrid({
			url:"../../api/searchAllTestItem?testStandardID="+testStandardID+"&itemName="+itemName,//加载的URL
			method:"get",
		    idField:"item_id",
		    rownumbers: true, //是否加行号
		    singleSelect:true,
			pagination:true,//显示分页
			pageSize:10,//分页大小
			pageList:[10,20,30,40],//每页的个数
			beforePageText: '第',//页数文本框前显示的汉字 
	        afterPageText: '页    共 {pages} 页', 
	        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录', 
			//fit:true,//自动补全
			width:1200,
            height:500,
            
			fitColumns:true,
			title:"标准项管理",
			columns:[[      //每个列具体内容
			  	{field:'ck',checkbox:true}, 
				{field:'item_id',title:'标准项ID',width:80//,hidden:true		    
				}, 
				{field:'template_id',title:'标准ID',width:100,hidden:true
				},
				{field:'template_name',title:'标准名称',width:200,},
				{field:'item_name',title:'标准项名称',width:130,},
				{field:'remark',title:'测试要求',width:100,hidden:true,
					formatter: function(value,row,index){
						keditor.html('');
						keditor.html(value);
						return keditor.text();
					}

				},	
				{field:'tool',title:'关联设备',width:80,}, 
				{field:'cost',title:'收费标准',width:100,}, 
				{field:'remarks',title:'备注',width:70,},
				{field:'judgingBasis',title:'判定依据',width:100,}, 
				{field:'additionalCharge',title:'附加费用',width:80,}, 
				{field:'chargeUnit',title:'费用单位',width:80,},
				{field:'isrt_emp_id',title:'添加人工号',width:100,hidden:true
				},
				{field:'isrt_emp_name',title:'添加人',width:70,},
				{field:'update_emp_id',title:'最后修改人工号',width:100,hidden:true
				},
				{field:'update_emp_name',title:'最后修改人',width:100,},
				{field:'isrt_date',title:'添加时间',width:100,},
				{field:'update_date',title:'最后修改时间',width:100,},
				{field:'delete_status',title:'标志',width:100,hidden:true	    
				},
            ]],
            
			toolbar:[              //工具条
		        {id:"tool_add",text:"增加",iconCls:"icon-ktsyAdd",handler:function(){//回调函数
		   			var t1 = 1;
		        	window.location.href="../../menu_nj/TestStandardManage/TestStandardSubitem?id="+0+"&action="+t1+"&tstId="+testStandardID;
		        }},
		        {id:"tool_del",text:"删除",iconCls:"icon-ktsyDelete",handler:function(){
		        	var rows=datagrid.datagrid('getSelections');
		  			var itemID = rows[0]["item_id"];
		  			console.log(itemID);
		        	if(rows.length<=0)
		        	{
		        		$.messager.alert('警告','您没有选择','error');
		        	}
		        	else if(rows.length>1)
		        	{
		        		$.messager.alert('警告','不支持批量删除','error');
		        	}
		        	else
		        	{
		        		$.messager.confirm('确定','您确定要删除吗',function(t){
		        			if(t)
		        			{
		        				
		        				$.ajax({
		        					url : '../../api/deleteTestItem',
		        					data : rows[0],
		        					dataType : 'json',
		        					success : function(r) {
		        						if (r==1) {
		        							datagrid.datagrid('acceptChanges');
		        							$.messager.show({
		        								msg : r.msg,
		        								title : '成功'
		        							});
		        							editRow = undefined;
		        							datagrid.datagrid('reload');

		        							//操作记录
		        							saveSystemLog("标准项维护","www.ktsy.com/menu_nj/TestStandardManage/TestItemManage","4","删除标准项",realname,userno);
		        						} else {
		        							/*datagrid.datagrid('rejectChanges');*/
		        							datagrid.datagrid('beginEdit', editRow);
		        							$.messager.alert('失败', '删除失败!存在关联测试子项', 'error',function(){
		        								$.messager.confirm('提示','是否查看关联测试项',function(r){
													if (r){
													    console.log("标准项ID："+itemID);
													    
													    window.location.href="../../menu_nj/TestStandardManage/TestStandardSubitem?id="+itemID+"&action="+3;
													}
												});
		        							});
		        						}
		        						datagrid.datagrid('unselectAll');
		        					}
		        				});
		        			
		        			}
		        		})
		        	}
		        	
		        	
		        }},
		        {id:"tool_edit",text:"修改",iconCls:"icon-ktsyEdit",handler:function(){		        	
		        	var rows=datagrid.datagrid('getSelections');
		        	editUrl="../../api/editTestItem";			    		
		   			
		    		if(rows.length<=0)
		        	{
		        		$.messager.alert('警告','您没有选择','error');
		        	}
		        	else if(rows.length>1)
		        	{
		        		$.messager.alert('警告','不支持批量修改','error');
		        	}
		        	else
		        	{
		        		var t2 = 2;
		        		window.location.href="../../menu_nj/TestStandardManage/TestStandardSubitem?id="+rows[0]["item_id"]+"&action="+t2;
		        	}
		        }},
		        {id:"tool_view",text:"查看",iconCls:"icon-ktsySearch",handler:function(){
		        	var rows=datagrid.datagrid('getSelections');
		  
		        	if(rows.length<=0)
		        	{
		        		$.messager.alert('警告','您没有选择','error');
		        	}
		        	else if(rows.length>1)
		        	{
		        		$.messager.alert('警告','不支持批量查看','error');
		        	}
		        	else
		        	{
		        		var index=datagrid.datagrid('getRowIndex',datagrid.datagrid('getSelected'))
              			var row1 = datagrid.datagrid('getData').rows[index]; 
              			//alert(row1.id);
              			//alert(row1.);
              			var t3 = 3;
              			console.log(row1.item_id);
              			window.location.href="../../menu_nj/TestStandardManage/TestStandardSubitem?id="+row1.item_id+"&action="+t3;
              			//window.location.href="../../menu/TestStandardManage/TestStandardSubitem?id="+row1.id;
		        	}
		        }},
		        {id:"tool_import",text:"批量导入",iconCls:"icon-ktsyCopy",handler:function(){
		        	$('#importExcel').window('open').window('move',{
		        		left:450,
		        		top:100
		        	});
		        }}
			       
		    ], 
		});

    	datagrid.datagrid('sort', 'item_id');  //排序  

		$("#search").click(function(){
			datagrid.datagrid('load',{
				text:$("#text").val()
			});

		});		
	});

$(document).ready(function() {
		$("#saveButton").click(function saveInfo() {
			var param = $("#ef").serializeArray();
			//alert(param);
			// var d = $('#validdeadline').datetimebox('getValue');
			console.log(param);
			$.ajax({
				url: editUrl,
				type: 'get',
				dataType: 'json',
				data: param,
				success:function(r){
					if (r==1) {
						datagrid.datagrid('acceptChanges');
						$.messager.show({
							msg : r.msg,
							title : '成功'
						});
					}
				},
				error:function() {
					$.messager.alert('错误',"错误"+param,"error");
				},
				complete:function(){
					$('#edit').dialog('close');
					location.reload();
					// datagrid.datagrid('reload');
				}			
			});			
		});
	});

function searchByConditions(){
		console.log('start search');
		var testStandardID = $('#testStandard').combobox('getValue');
		console.log(testStandardID);
		var itemName = $('#itemName').searchbox('getValue');
		
		console.log(itemName);
		if (testStandardID!=null && testStandardID!='') {
			$('#dg').datagrid('reload',"../../api/searchAllTestItem?testStandardID="+testStandardID+"&itemName="+itemName);
		}
		else{
			testStandardID = 0;
			$('#dg').datagrid('reload',"../../api/searchAllTestItem?testStandardID="+testStandardID+"&itemName="+itemName);
		}
		
	}

	//记录用户登录系统日志
	function saveSystemLog(pagetitle,page,operation,detail,userrealname,userno){
		// alert("保存日志信息");
		$.ajax({
	        type:"get",
	        dataType : "json",
	        url:"../../api/insertSystemLog",
	        data:{pagetitle:pagetitle,page:page,operation:operation,detail:detail,userrealname:userrealname,userno:userno},
	        success:function(r){
	            if (r==1){
	            	console.log("保存"+r+"条日志信息");
	          	}
	        }
      	});
	}

	function importExcel1(){
           			console.log(realname);
           			var file1 = document.getElementById("imprtFile1").value;
           			if(file1 !=null && file1 != "" && file1 != undefined){
           				$.ajaxFileUpload(
		                    {
		                        url:'/importExcel?flag=tit&realname='+realname,            //需要链接到服务器地址
		                        secureuri:false,
		                        fileElementId:'imprtFile1',                        //文件选择框的id属性
		                        dataType: 'json',                                     //服务器返回的格式，可以是json
		                        success: function (data, status){
		                          // datagrid0.datagrid('reload');
		                            $.messager.alert('信息',"导入成功","info");
		                            $('#importExcel').dialog('close');
									datagrid.datagrid('reload');
		                        },
		                        error: function(data, status, e){
		                            $.messager.alert('错误',"上传失败!","error");
		                        }
		                    }
			                  
			            );
           			}else{
           				$.messager.alert('错误',"请选择上传文件！","error");
           			}
		            
		           
		        }

		        function importExcel2(){
           			console.log(realname);
           			var file2 = document.getElementById("imprtFile2").value;
           			if(file2 !=null && file2 != "" && file2 != undefined){
           				$.ajaxFileUpload(
		                    {
		                        url:'/importExcel?flag=tss&realname='+realname,            //需要链接到服务器地址
		                        secureuri:false,
		                        fileElementId:'imprtFile2',                        //文件选择框的id属性
		                        dataType: 'json',                                     //服务器返回的格式，可以是json
		                        success: function (data, status){
		                          // datagrid0.datagrid('reload');
		                            $.messager.alert('信息',"导入成功","info");
		                            $('#importExcel').dialog('close');
		                        },
		                        error: function(data, status, e){
		                            $.messager.alert('错误',"上传失败!","error");
		                        }
		                    }
		                  
		            	);
           			}else{
           				$.messager.alert('错误',"请选择上传文件！","error");
           			}		            
		           
		        }